package com.hand.api.controller.v1;

import com.hand.app.service.SoLineService;
import com.hand.config.SwaggerApiConfig;
import com.hand.domain.entity.SoLine;
import com.hand.domain.repository.SoLineRepository;
import io.choerodon.core.domain.Page;
import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.PageHelper;
import io.choerodon.mybatis.pagehelper.annotation.SortDefault;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.mybatis.pagehelper.domain.Sort;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hzero.core.base.BaseController;
import org.hzero.core.util.Results;
import org.hzero.mybatis.helper.SecurityTokenHelper;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

/**
 * 订单表 管理 API
 */
@Api(tags = SwaggerApiConfig.SO_LINE)
@RestController("soLineController.v1")
@RequestMapping("/v1/{organizationId}/so-lines")
public class SoLineController extends BaseController {

    @Resource
    private SoLineRepository soLineRepository;
    @Resource
    private SoLineService soLineService;

    @ApiOperation(value = "创建订单表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PostMapping
    public ResponseEntity<SoLine> create(@RequestBody SoLine soLine, @PathVariable String organizationId) {
        validObject(soLine);
        soLineRepository.insertSelective(soLine);
        return Results.success(soLine);
    }

    @ApiOperation(value = "修改订单表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PutMapping
    public ResponseEntity<SoLine> update(@RequestBody SoLine soLine, @PathVariable String organizationId) {
        SecurityTokenHelper.validToken(soLine);
        soLineRepository.updateByPrimaryKeySelective(soLine);
        return Results.success(soLine);
    }

    /**
     * 订单行查询API
     * @param pageRequest
     * @param soHeaderId
     * @param organizationId
     * @return
     */
    @ApiOperation(value = "订单行查询API")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping("/{soHeaderId}")
    public ResponseEntity<Page<SoLine>> queryOrderLineForAll(
            @ApiIgnore @SortDefault(value = SoLine.FIELD_LINE_NUMBER, direction = Sort.Direction.DESC) PageRequest pageRequest,
            @PathVariable Long soHeaderId,
            @PathVariable String organizationId) {

        Page<SoLine> list = PageHelper.doPageAndSort(pageRequest,()->{
           return soLineRepository.select(SoLine.FIELD_SO_HEADER_ID,soHeaderId);
        });
        return Results.success(list);
    }

    /**
     * 订单行删除API
     * @param soLineId
     * @param organizationId
     * @return
     */
    @ApiOperation(value = "订单行删除API")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @DeleteMapping("/{soLineId}")
    public ResponseEntity<?> queryOrderHeader(@PathVariable Long soLineId, @PathVariable String organizationId) {
        soLineService.deleteOrderHeader(soLineId);
        return Results.success();
    }

}
